/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.model;

import java.util.HashSet;
import java.util.Set;


/**
 * Knowledge unit. Persistent class, read-write.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 22-mar-2009 11:04:04
 */
public class KnowledgeUnit implements java.io.Serializable {
	/**
	 * The identifier.
	 */
	private long id;

	/**
	 * The knowledge unit code, must be unique.
	 */
	private String code;

	/**
	 * The name.
	 */
	private String name;

	/**
	 * The description.
	 */
	private String description;

	/**
	 * The owner organization.
	 */
	private Organization organization;

	/**
	 * The qualifications associated with a unit.
	 */
	private Set<Qualification> qualifications;

	/**
	 * The training actions associated with a unit.
	 */
	private Set<TrainingAction> trainings;

	/**
	 * The knowledge paths associated with a unit.
	 */
	private Set<KnowledgePath> paths;

	/**
	 * The questions.
	 */
	private Set<Question> questions;

/**
   * Constructor.
   */
	public KnowledgeUnit() {
		qualifications = new HashSet<Qualification>();
		trainings = new HashSet<TrainingAction>();
		paths = new HashSet<KnowledgePath>();
		questions = new HashSet<Question>();
	}

	/**
	 * Retrieves the identifier.
	 *
	 * @return the identifier.
	 */
	public long getId() {
		return this.id;
	}

	/**
	 * Sets the identifier.
	 *
	 * @param id the identifier.
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * Retrieves the code.
	 *
	 * @return the code.
	 */
	public String getCode() {
		return this.code;
	}

	/**
	 * Sets the code.
	 *
	 * @param code the code.
	 */
	public void setCode(final String code) {
		this.code = code;
	}

	/**
	 * Retrieves the code.
	 *
	 * @return the name.
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * Sets the code.
	 *
	 * @param name the name.
	 */
	public void setName(final String name) {
		this.name = name;
	}

	/**
	 * Retrieves the description.
	 *
	 * @return the description.
	 */
	public String getDescription() {
		return this.description;
	}

	/**
	 * Sets the description.
	 *
	 * @param description the description.
	 */
	public void setDescription(final String description) {
		this.description = description;
	}

	/**
	 * Retrieves the organization.
	 *
	 * @return the organization.
	 */
	public Organization getOrganization() {
		return this.organization;
	}

	/**
	 * Sets the organization.
	 *
	 * @param organization the organization.
	 */
	public void setOrganization(final Organization organization) {
		this.organization = organization;
	}

	/**
	 * Retrieves the qualifications.
	 *
	 * @return the qualifications.
	 */
	public Set<Qualification> getQualifications() {
		return this.qualifications;
	}

	/**
	 * Sets the qualifications.
	 *
	 * @param qualifications the qualifications.
	 */
	public void setQualifications(final Set<Qualification> qualifications) {
		this.qualifications = qualifications;
	}

	/**
	 * Retrieves the trainings.
	 *
	 * @return the trainings.
	 */
	public Set<TrainingAction> getTrainings() {
		return this.trainings;
	}

	/**
	 * Sets the trainings.
	 *
	 * @param trainings the trainings.
	 */
	public void setTrainings(final Set<TrainingAction> trainings) {
		this.trainings = trainings;
	}

	/**
	 * Retrieves the paths.
	 *
	 * @return the paths.
	 */
	public Set<KnowledgePath> getPaths() {
		return this.paths;
	}

	/**
	 * Sets the paths.
	 *
	 * @param paths the paths.
	 */
	public void setPaths(final Set<KnowledgePath> paths) {
		this.paths = paths;
	}

	/**
	 * Retrieves the questions.
	 *
	 * @return the questions.
	 */
	public Set<Question> getQuestions() {
		return questions;
	}

	/**
	 * Sets the questions.
	 *
	 * @param questions the questions.
	 */
	public void setQuestions(final Set<Question> questions) {
		this.questions = questions;
	}

	/**
	 * Adds a qualification.
	 *
	 * @param qualification a qualification.
	 */
	public void add(final Qualification qualification) {
		getQualifications().add(qualification);
	}

	/**
	 * Removes a qualification.
	 *
	 * @param qualification a qualification.
	 */
	public void remove(final Qualification qualification) {
		getQualifications().remove(qualification);
	}

	/**
	 * Adds a training action.
	 *
	 * @param action a training action.
	 */
	public void add(final TrainingAction action) {
		getTrainings().add(action);
	}

	/**
	 * Removes a training action.
	 *
	 * @param action a training action.
	 */
	public void remove(final TrainingAction action) {
		getTrainings().remove(action);
	}

	/**
	 * Adds a knowledge path.
	 *
	 * @param path a knowledge path.
	 */
	public void add(final KnowledgePath path) {
		getPaths().add(path);
	}

	/**
	 * Removes a knowledge path.
	 *
	 * @param path a knowledge path.
	 */
	public void remove(final KnowledgePath path) {
		getPaths().remove(path);
	}

	/**
	 * Adds a question.
	 *
	 * @param question a question.
	 */
	public void add(final Question question) {
		question.setUnit(this);
		getQuestions().add(question);
	}

	/**
	 * Removes a question.
	 *
	 * @param question a question.
	 */
	public void remove(final Question question) {
		question.setUnit(null);
		getQuestions().remove(question);
	}

	/**
	 * Checks if objects are equal.
	 *
	 * @param o other object.
	 *
	 * @return true if equal, false otherwise.
	 */
	@Override
	public boolean equals(Object o) {
		if(this == o) {
			return true;
		}

		if(!(o instanceof KnowledgeUnit)) {
			return false;
		}

		KnowledgeUnit that = (KnowledgeUnit)o;

		if(getId() != that.getId()) {
			return false;
		}

		return true;
	}

	/**
	 * Calculates the hash of this instance.
	 *
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		return (int)(getId() ^ (getId() >>> 32));
	}

	/**
	 * Stringifies this instance.
	 *
	 * @return the string representation.
	 */
	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder();

		sb.append("KnowledgeUnit");
		sb.append("{id=").append(getId());
		sb.append(", code='").append(getCode()).append('\'');
		sb.append('}');

		return sb.toString();
	}
}
